﻿using System.Data;
using Management.Domain.IRepository;
using Microsoft.Extensions.Configuration;
using MySqlConnector;

namespace Management.EntityFrameworkCore.Repository;

public class EFRepository<TEntity> : IRepository<TEntity> where TEntity : class, new()
{
    private readonly IConfiguration mConfiguration;

    public EFRepository(IConfiguration configuration)
    {
        mConfiguration = configuration;
    }

    #region OpenConnection

    /// <summary>
    /// 创建数据库连接，并打开连接
    /// </summary>
    /// <returns>IDbConnection</returns>
    public IDbConnection GetOpenConn()
    {
        string connectionString = mConfiguration.GetConnectionString("MySqlConnectionString");

        IDbConnection lConnection = new MySqlConnection(connectionString);
        try
        {
            lConnection.Open();
        }
        catch (Exception ex)
        {
            throw new Exception("数据库连接错误:" + ex.Message);
        }

        return lConnection;
    }

    #endregion

    #region 查询

    public TEntity GetById(int id)
    {
        return default;
    }

    public TEntity QueryFirstOrDefault(string sql, object param)
    {
        return default;
    }

    public IEnumerable<TEntity> Query(string sql, object param)
    {
        return default;
    }

    #endregion
}